package com.catazy.mvvmlib.base

import android.app.Application
import androidx.lifecycle.ProcessLifecycleOwner
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.LogUtils.Config
import com.catazy.mvvmlib.ext.lifecycle.ActivityLifecycleCallbacksImpl
import com.catazy.mvvmlib.ext.lifecycle.AppLifecycleObserver

/**
 * 作者: Tao
 * 日期: 2024/1/24
 * 时间: 16:57
 * 描述: Application的基类
 * 主要作用: 提供一个全局级别的ViewModel
 */
abstract class BaseApplication : Application() {

    companion object {
        lateinit var INSTANCE: BaseApplication
    }


    override fun onCreate() {
        super.onCreate()
        INSTANCE = this
        // 初始化日志打印器
        val config = LogUtils.getConfig()
        config.isLogSwitch = isDebug()
        // 是否监听Activity生命周期
        if (isWatchActivityLife()) {
            registerActivityLifecycleCallbacks(ActivityLifecycleCallbacksImpl())
        }
        // 是否监听应用前后台
        if (isWatchAppLife()) {
            ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver)
        }
    }

    /**
     * 是否是Debug模式
     * @return true:是 false:否 ,默认false
     */
    open fun isDebug(): Boolean = false

    /**
     * 是否监听Activity生命周期
     * @return true:是 false:否 ,默认false
     */
    open fun isWatchActivityLife(): Boolean = false

    /**
     * 是否监听应用前后台
     * @return true:是 false:否 ,默认false
     */
    open fun isWatchAppLife(): Boolean = false
}